package com.example.bbs.controller;

import com.example.bbs.http.bean.HttpSendBasic;
import com.example.bbs.http.bean.comment.CommentSend;
import com.example.bbs.http.bean.note.NoteIndexListSend;
import com.example.bbs.http.bean.note.NoteInit;
import com.example.bbs.http.bean.note.NoteListSend;
import com.example.bbs.http.bean.type.HomePageData;
import com.example.bbs.service.NoteService;
import com.example.bbs.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/note")
@Api(tags = {"帖子接口"})
//@ResponseBody
public class NoteController {

    @Autowired
    private NoteService noteService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/index")
    @ApiOperation("板块加载")
    public String noteIndex(@RequestParam("typeid") Integer typeid, Model model) {
        //加载首页列表
        NoteIndexListSend noteIndexListSend = noteService.getIndexList(typeid);
        model.addAttribute("notes", noteIndexListSend);
        //nav信息
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "notelist";
    }


    @GetMapping("/page")
    @ApiOperation("板块下一页")
    public String notePage(@RequestParam("typeid") Integer typeid, @RequestParam("page") Integer page,Model model) {
        //查找数据
        NoteListSend noteListSend = noteService.getPage(typeid, page);
        model.addAttribute("notes", noteListSend);
        //nav信息
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "notepage";
    }

    @GetMapping("/note")
    @ApiOperation("加载帖子")
    public String getNote(@RequestParam("noteid") Integer noteId,Model model) {
        //帖子详情信息
        NoteInit noteInit = noteService.getNote(noteId);
        model.addAttribute("note", noteInit);
        //nav信息
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "note";
    }

    @GetMapping("/comment")
    @ApiOperation("加载评论")
    public CommentSend getCommentPage(@RequestParam("noteid") Integer noteId, @RequestParam("page") Integer page) {
        return noteService.commentSend(noteId, page);
    }

    @GetMapping("/note/createby")
    @ApiOperation("根据用户查找帖子")
    public String getNotesByCreateby(@RequestParam("createby") Integer createby,Model model){
        Map<String,Object> notes = noteService.getNoteListByCreateBy(createby);
        model.addAttribute("notes", notes);
        //nav信息
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "personalCenter";
    }

    @GetMapping("/note/select")
    @ApiOperation("查找帖子")
    public String selectNote(@RequestParam("select") String select, @RequestParam("page") Integer page,Model model){
        //查找数据
        NoteListSend noteListSend = noteService.select(select, page);
        model.addAttribute("notes", noteListSend);
        model.addAttribute("select", select);
        //nav信息
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "notepage";
    }

}
